package org.mymail.mail;

import java.io.File;
import java.io.IOException;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
import noNamespace.FreskoSubscribersDocument;
import org.apache.xmlbeans.XmlException;
import noNamespace.FreskoSubscribersDocument.FreskoSubscribers;

public class UpdateSubscribers {

	static FreskoSubscribersDocument recipientsDoc;
	static FreskoSubscribers.Subscriber sub[];

	static {
		try {
			recipientsDoc = noNamespace.FreskoSubscribersDocument.Factory
					.parse(new File(MailConstants.recipientsXMLLoc));
			FreskoSubscribers s = recipientsDoc
					.getFreskoSubscribers();
			sub = s.getSubscriberArray();
		} catch (XmlException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void removeSubscriber(Address address, int mailFwdType) {
		InternetAddress address2 = (InternetAddress) address;
		for (int i = 0; i < sub.length; i++) {
			FreskoSubscribers.Subscriber subscriber = sub[i];
			if (subscriber.getId().equalsIgnoreCase(address2.getAddress())) {
				FreskoSubscribers.Subscriber.SubscribeType subType[] = subscriber
						.getSubscribeTypeArray();
				for (int j = 0; j < subType.length; j++) {
					FreskoSubscribers.Subscriber.SubscribeType subscribeType = subType[j];
					if (subscribeType.getId() == mailFwdType) {
						subscribeType.setId(-1);
						try {
							recipientsDoc.save(new File(
									MailConstants.recipientsXMLLoc));
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}

			}
		}

	}

	
}